package cn.com.wxd.action.user;

import javax.servlet.http.HttpServletRequest;

import cn.com.wxd.action.ValidatorResult;
import cn.com.wxd.common.CharUtil;

/**
 * 用户模块功能的验证
 * Created by ComXu on 2017/5/19.
 */
class UserCtlValidator extends ValidatorResult {

    static ValidatorResult ajaxLoginValidator(HttpServletRequest request, String userName, String passWord,
                                              String verCode) {
        if (CharUtil.isEmpty(userName)) {
            return failuerValidatorResult("用户名不能为空!");
        }
        if (userName.length() < 6) {
            return failuerValidatorResult("用户名长度不能少于6!");
        }
        if (passWord == null || passWord.trim().equals("")) {
            return failuerValidatorResult("密码不能为空!");
        }
        if (verCode == null || verCode.trim().equals("")) {
            return failuerValidatorResult("验证码不能为空!");
        }
        String verCodesession = (String) request.getSession().getAttribute("vercode");
        request.getSession().removeAttribute("vercode");  //获取后立马移除
        if (verCodesession == null) {
            verCodesession = "";
        }
        if (!verCodesession.equalsIgnoreCase(verCode.trim())) {
            return failuerValidatorResult("验证码错误!");
        }
        return ValidatorResult.successValidatorResult();
    }

    static ValidatorResult ajaxRegistValidator(HttpServletRequest request, String userName, String passWord,
                                               String repassWord,
                                               String verCode,
                                               String registType,
                                               String verPhone,
                                               String verEmail) {
        if (CharUtil.isEmpty(userName)) {
            return failuerValidatorResult("用户名不能为空!");
        }
        if (userName.length() < 6) {
            return failuerValidatorResult("用户名长度不能小于6！");
        }
        if (userName.length() != CharUtil.stringLength(userName)) {
            return failuerValidatorResult("用户名不能含有中文符号！");
        }
        if (CharUtil.isEmpty(passWord)) {
            return failuerValidatorResult("密码不能为空！");
        }
        if (!passWord.equals(repassWord)) {
            return failuerValidatorResult("两次输入密码不一致！");
        }
        if (CharUtil.isEmpty(verCode)) {
            return failuerValidatorResult("验证码不能为空！");
        }
        String verCodesession = (String) request.getSession().getAttribute("vercode");
        request.getSession().removeAttribute("vercode");  //获取后立马移除
        if (verCodesession == null) {
            verCodesession = "";
        }
        if (!verCodesession.equalsIgnoreCase(verCode.trim())) {
            return failuerValidatorResult("验证码错误！");
        }
        String dynamicVercode = (String) request.getSession().getAttribute("USERREG-CODE");
        if (dynamicVercode == null) {
            dynamicVercode = "";
        }
        if (registType != null) {
            if (registType.equalsIgnoreCase("phone")) {
                String timeStamp = (String) request.getSession().getAttribute("verphone" + userName);
                if (CharUtil.isEmpty(timeStamp)) {
                    return ValidatorResult.failuerValidatorResult("手机验证码错误！");
                } else {
                    if (!dynamicVercode.equalsIgnoreCase(verPhone)) {
                        return ValidatorResult.failuerValidatorResult("动态验证码错误！");
                    }
                }
            } else if (registType.equalsIgnoreCase("email")) {
                String timeStamp = (String) request.getSession().getAttribute("veremail" + userName);
                if (timeStamp == null) {
                    return ValidatorResult.failuerValidatorResult("邮箱验证码错误！");
                } else {
                    if (!dynamicVercode.equalsIgnoreCase(verEmail)) {
                        return ValidatorResult.failuerValidatorResult("动态验证码错误！");
                    }
                }
            } else {
                return failuerValidatorResult("请求错误，请刷新后重试！");
            }
        } else {
            return failuerValidatorResult("请求错误，请刷新后重试！");
        }
        return successValidatorResult();
    }
}